package com.hd.service.activiti.impl;

import com.hd.dao.BaseDao;
import com.hd.entity.activiti.ProcessType;
import com.hd.service.activiti.ProcessTypeService;
import com.hd.util.StringUtils;
import com.hd.util.result.PageInfo;
import com.hd.util.result.Tree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
/**
* 流程类型
*/
@Service("processTypeService")
public class ProcessTypeServiceImpl implements ProcessTypeService {
    @Autowired
    private BaseDao<ProcessType,String>processTypeDao;
    @Override
    public void save(ProcessType processType) {
    	processType.setId(UUID.randomUUID().toString());
       processTypeDao.save(processType);
    }

   
    @Override
    public void delete(String id){
      processTypeDao.delete(ProcessType.class,id);
    }

	@Override
	public void deleteByProcDefId(String procDefId) {
		String hql="delete from ProcessType where procdefid=?";
		processTypeDao.execute(hql,procDefId);
	}

	@Override
    public void update(ProcessType processType) {
        processTypeDao.update(processType);
    }
    @Override
    public ProcessType get(String id){
    	return processTypeDao.get(ProcessType.class, id);
    }
        

    @Override
    public void selectDataGrid(PageInfo pageInfo) {
        String hql="from ProcessType where 1=1 and status=1 ";
        Map<String,Object>condition=pageInfo.getCondition();
        List<Object> params=new ArrayList<Object>();
        if(StringUtils.isNotNull(condition.get("name"))) {
        	hql+=" and name like ?";
        	params.add("%"+condition.get("name")+"%");
        }
        if(!pageInfo.getSort().equals("")) {
            hql+=" order by "+pageInfo.getSort()+" "+pageInfo.getOrder();
        }
        List<ProcessType> processTypes=processTypeDao.findPage(hql, pageInfo.getNowpage(), pageInfo.getPagesize(),params);
        pageInfo.setRows(processTypes);
		String countHql="select count(*) "+hql;
		int total=Integer.parseInt(processTypeDao.getCountByList(countHql,params).toString());
        pageInfo.setTotal(total);
    }

	@Override
	public List<ProcessType> findAll() {
		 String hql="from ProcessType where 1=1 and status=1 ";
		return processTypeDao.find(hql);
	}

	@Override
	public List<ProcessType> selectTreeGrid(ProcessType processType) {
		List<Object>params=new ArrayList<Object>();
		String hql = "from ProcessType where 1=1 and status=1 ";
		if(StringUtils.isNotNull(processType.getName())) {
			hql+=" and name like ?";
        	params.add("%"+processType.getName()+"%");
		}
		hql+=" order by seq asc";
		return processTypeDao.findByList(hql,params);
	}

	@Override
	public List<Tree> selectTree(ProcessType processType1) {
		List<ProcessType> processTypes = selectTreeGrid(processType1);
		List<Tree> trees = new ArrayList<Tree>();
		if (processTypes != null) {
			for (ProcessType processType : processTypes) {
				Tree tree = new Tree();
				tree.setId(processType.getId().toString());
				tree.setText(processType.getName());
				tree.setIconCls("fi-folder");
				tree.setPid(StringUtils.isNull(processType.getPid())?"":processType.getPid().toString());
				trees.add(tree);
			}
		}
		return trees;
	}


	@Override
	public ProcessType getByKey(String key) {
		String hql="from ProcessType where 1=1 and status=1 and key=? order by seq";
		List<ProcessType>vos=processTypeDao.find(hql, key);
		if(vos.size()>0) {
			return vos.get(0);
		}
		return null;
	}


	@Override
	public List<ProcessType> findByType(Integer type) {
		String hql="from ProcessType where type=? and status=1";
		return processTypeDao.find(hql, type);
	}
	@Override
	public List<ProcessType> findByType(Integer type,String name) {
		String hql="from ProcessType where and status=1 type=? and name like ?";
		return processTypeDao.find(hql, type,"%"+name+"%");
	}

	@Override
	public void cancel(String deploymentId) {
		String hql="update ProcessType set status=0 where deploymentId=?";
		processTypeDao.execute(hql,deploymentId);
	}
}
